//
//  WeatherViewController.m
//  MINFOBBS
//
//  Created by 蔡龙生 on 13-11-19.
//  Copyright (c) 2013年 minfo. All rights reserved.
//

#import "WeatherViewController.h"
#import "WeatherIndexViewController.h"
#import "MainSearchViewController.h"
@interface WeatherViewController (){
    UIColor *backColor;
}

@end

@implementation WeatherViewController



- (void)viewDidLoad
{
    [super viewDidLoad];

    if(!self.title) self.title = @"百事搜";
    [self.navigationController setNavigationBarHidden:YES];
     weatherInfo =[[self.arrReturn objectForKey:@"Items"] objectForKey:@"Item"] ;
    backColor= [UIColor colorWithRed:243/255.0 green:243/255.0 blue:243/255.0 alpha:1];
    self.viewCity.backgroundColor=backColor;
//    self.viewMain.backgroundColor=backColor;
//    [self GetWeatherSearch:@"天气" Location:@"上海"];
    [self GetCurrentDayWeather];
    [self GetOtherDayWeather];

}


//当天天气情况
-(void)GetCurrentDayWeather{
    //日期
    NSArray *list=[[[weatherInfo objectAtIndex:0] objectForKey:@"Properties"] objectForKey:@"Property"];
    NSString *date=[[NSString alloc] init];     //获取日期数据
    date=[[list objectAtIndex:9] objectForKey:@"text"];
    
    self.dateLbl.text=[self DateFormat:date];
    //颜色值＃0181d4
    UIColor *blueColor= [UIColor colorWithRed:1/255.0 green:129/255.0 blue:212/255.0 alpha:1];
    self.dateLbl.textColor=blueColor;
    self.dateLbl.font=[UIFont systemFontOfSize:15];
    
    
    //晴到多云
    NSString *weather;
    NSString *dayWeather=[[list objectAtIndex:0] objectForKey:@"text"];//白天天气
    NSString *nightWeather=[[list objectAtIndex:1] objectForKey:@"text"];//夜间天气
    //天气信息
    UIImageView *dWeatherImg=[[UIImageView alloc]initWithFrame:CGRectMake(27, 46, 64, 60)];
    dWeatherImg.image=[UIImage imageNamed:[self WeatherImg:dayWeather]];//天气符号
    
    if([dayWeather isEqualToString:nightWeather])
    {
        weather=dayWeather;
        [self.viewMain addSubview:dWeatherImg];
    }
    else
    {
        UIImageView *nWeatherImg=[[UIImageView alloc]initWithFrame:CGRectMake(27, 46, 64, 60)];
        nWeatherImg.image=[UIImage imageNamed:[self WeatherImg:nightWeather]];
        weather=[[NSString alloc] initWithFormat:@"%@-%@",dayWeather,nightWeather];
        [self.viewMain addSubview:dWeatherImg];
        [self.viewMain addSubview:nWeatherImg];
    }
    self.weatherLbl.text=weather;
    self.weatherLbl.backgroundColor=[UIColor clearColor];
    self.weatherLbl.font=[UIFont systemFontOfSize:13.5];
    
    //星期几
    NSString *tWhatDay=[[list objectAtIndex:10] objectForKey:@"text"];
    //周几
    NSString *whatDay=[[NSString alloc] initWithFormat:@"周%@",[tWhatDay substringWithRange:NSMakeRange(2, 1)]];
    self.whatDayLbl.text=whatDay;
    self.whatDayLbl.textColor=blueColor;
    self.whatDayLbl.font=[UIFont systemFontOfSize:15];
    
    //温度
    NSString *minTemperature=[[list objectAtIndex:3] objectForKey:@"text"];//最低温度
    NSString *muxTemperature=[[list objectAtIndex:2] objectForKey:@"text"];//最高温度
    NSArray *tempTemperature=[minTemperature componentsSeparatedByString:@"℃"];
    NSString *tempNumber=[tempTemperature objectAtIndex:0];
    
    NSString *temperature=[[NSString alloc] initWithFormat:@"%@~%@",tempNumber,muxTemperature];
    self.temperatureLbl.text=temperature;
    self.temperatureLbl.backgroundColor=[UIColor clearColor];
    self.temperatureLbl.font=[UIFont systemFontOfSize:14.5];
    
    //白天风
    NSString *windDirection=[[list objectAtIndex:4] objectForKey:@"text"];  //风向
    NSString *windPower=[[list objectAtIndex:5] objectForKey:@"text"];      //风力
    NSString *dayWind=[[NSString alloc] initWithFormat:@"%@%@",windDirection,windPower];
    self.windLbl.text=dayWind;
    self.windLbl.backgroundColor=[UIColor clearColor];
    self.windLbl.font=[UIFont systemFontOfSize:13.5];
    
}

//后两天天气
-(void)GetOtherDayWeather{
    for (int i=0; i<2; i++) {
        NSArray *list=[[[weatherInfo objectAtIndex:i+1] objectForKey:@"Properties"] objectForKey:@"Property"];
        //颜色值＃0181d4
        UIColor *blueColor= [UIColor colorWithRed:1/255.0 green:129/255.0 blue:212/255.0 alpha:1];
        
        //周几
        NSString *tWhatDay=[[list objectAtIndex:10] objectForKey:@"text"];
        NSString *whatDay=[[NSString alloc] initWithFormat:@"周%@",[tWhatDay substringWithRange:NSMakeRange(2, 1)]];
        
        //天气信息
        UIView *nextView=[[UIView alloc] initWithFrame:CGRectMake(i*144, 135, 130, 200)];
        
        //晴到多云
        NSString *weather;
        NSString *dayWeather=[[list objectAtIndex:0] objectForKey:@"text"];//白天天气
        NSString *nightWeather=[[list objectAtIndex:1] objectForKey:@"text"];//夜间天气
        
        UIImageView *dWeatherImg=[[UIImageView alloc]initWithFrame:CGRectMake(41, 80, 64, 60)];
        dWeatherImg.image=[UIImage imageNamed:[self WeatherImg:dayWeather]];
        
        //weatherImg2.image=[UIImage imageNamed:@"qns_weather_duoy.png"];
        if([dayWeather isEqualToString:nightWeather])
        {
            weather=dayWeather;
            [nextView addSubview:dWeatherImg];
        }
        else
        {
            UIImageView *nWeatherImg=[[UIImageView alloc]initWithFrame:CGRectMake(41, 80, 64, 60)];
            nWeatherImg.image=[UIImage imageNamed:[self WeatherImg:nightWeather]];
            weather=[[NSString alloc] initWithFormat:@"%@-%@",dayWeather,nightWeather];
            [nextView addSubview:dWeatherImg];
            [nextView addSubview:nWeatherImg];
        }
        
        //温度
        NSString *minTemperature=[[list objectAtIndex:3] objectForKey:@"text"];//最低温度
        NSString *muxTemperature=[[list objectAtIndex:2] objectForKey:@"text"];//最高温度
        NSArray *tempTemperature=[minTemperature componentsSeparatedByString:@"℃"];
        NSString *tempNumber=[tempTemperature objectAtIndex:0];
        NSString *temperature=[[NSString alloc] initWithFormat:@"%@~%@",tempNumber,muxTemperature];
        
        //白天风
        NSString *windDirection=[[list objectAtIndex:4] objectForKey:@"text"];  //风向
        NSString *windPower=[[list objectAtIndex:5] objectForKey:@"text"];      //风力
        NSString *dayWind=[[NSString alloc] initWithFormat:@"%@%@",windDirection,windPower];

    
        UILabel *whatDayLbl=[[UILabel alloc] initWithFrame:CGRectMake(23, 50, 100, 21)];
        whatDayLbl.text=whatDay;
        whatDayLbl.textColor=blueColor;
        whatDayLbl.textAlignment=NSTextAlignmentCenter;
        whatDayLbl.font=[UIFont systemFontOfSize:13.5];
        whatDayLbl.userInteractionEnabled=YES;
        [nextView addSubview:whatDayLbl];
        
        UILabel *weatherLbl=[[UILabel alloc] initWithFrame:CGRectMake(23, 145, 100, 21)];
        weatherLbl.text=weather;
        weatherLbl.backgroundColor=[UIColor clearColor];
        weatherLbl.textAlignment=NSTextAlignmentCenter;
        weatherLbl.font=[UIFont systemFontOfSize:12.5];
        weatherLbl.userInteractionEnabled=YES;
        [nextView addSubview:weatherLbl];
        
        UILabel *temperatureLbl=[[UILabel alloc] initWithFrame:CGRectMake(23,170,100,21)];
        temperatureLbl.text=temperature;
        temperatureLbl.backgroundColor=[UIColor clearColor];
        temperatureLbl.textAlignment=NSTextAlignmentCenter;
        temperatureLbl.font=[UIFont systemFontOfSize:12.5];
        temperatureLbl.userInteractionEnabled=YES;
        [nextView addSubview:temperatureLbl];
        
        UILabel *windLbl=[[UILabel alloc] initWithFrame:CGRectMake(23, 195, 100, 21)];
        windLbl.text=dayWind;
        windLbl.backgroundColor=[UIColor clearColor];
        windLbl.textAlignment=NSTextAlignmentCenter;
        windLbl.font=[UIFont systemFontOfSize:12.5];
        windLbl.userInteractionEnabled=YES;
        [nextView addSubview:windLbl];
        
        [self.view addSubview:nextView];
        
//        [self.view setBackgroundColor:backColor];
    }
    
    btnIndex = [UIButton buttonWithType:UIButtonTypeCustom];
    [btnIndex addTarget:self action:@selector(GotoWeatherIndex:) forControlEvents:UIControlEventTouchUpInside];
    [btnIndex setFrame:CGRectMake(127,500,100,30)];
    [btnIndex setTitle:@"今日指数" forState:UIControlStateNormal];
    UIImage *btnImg=[UIImage imageNamed:[NSString stringWithFormat:@"fasong"]];
    [btnIndex setBackgroundImage:btnImg forState:(UIControlStateNormal)];
    btnIndex.tag=400;
    [self.view addSubview:btnIndex];
}

-(NSString *)WeatherImg:(NSString *) Weather{
    NSString *weatherImg=[[NSString alloc] init];
    if ([Weather isEqualToString:@"晴转多云"]) {
        weatherImg=@"qns_weather_duoy";
    } else if ([Weather isEqualToString:@"阵雨"]) {
        weatherImg=@"qns_weather_leizy";
    } else if ([Weather isEqualToString:@"大雨"]) {
        weatherImg=@"qns_weather_day";
    } else if ([Weather isEqualToString:@"小到中雨"]) {
        weatherImg=@"qns_weather_zhongy";
    } else if ([Weather isEqualToString:@"中雨"]) {
        weatherImg=@"qns_weather_zhongy";
    } else if ([Weather isEqualToString:@"阵雪"]) {
        weatherImg=@"qns_weather_zhongx";
    } else if ([Weather isEqualToString:@"晴"]) {
        weatherImg=@"qns_weather_qing";
    } else if ([Weather isEqualToString:@"大到暴雨"]) {
        weatherImg=@"qns_weather_baoy";
    } else if ([Weather isEqualToString:@"雨夹雪"]) {
        weatherImg=@"qns_weather_yujx";
    } else if ([Weather isEqualToString:@"阴有小雨转多云"]) {
        weatherImg=@"qns_weather_duoy";
    } else if ([Weather isEqualToString:@"小到中雪"]) {
        weatherImg=@"qns_weather_zhongx";
    } else if ([Weather isEqualToString:@"中雪"]) {
        weatherImg=@"qns_weather_zhongx";
    } else if ([Weather isEqualToString:@"小雨"]) {
        weatherImg=@"qns_weather_xiaoy";
    } else if ([Weather isEqualToString:@"中到大雨"]) {
        weatherImg=@"qns_weather_day";
    } else if ([Weather isEqualToString:@"雷阵雨"]) {
        weatherImg=@"qns_weather_leizy";
    } else if ([Weather isEqualToString:@"阴"]) {
        weatherImg=@"qns_weather_yin";
    } else if ([Weather isEqualToString:@"小雪"]) {
        weatherImg=@"qns_weather_xiaox";
    } else if ([Weather isEqualToString:@"阴有小雨夹雪转多云"]) {
        weatherImg=@"qns_weather_duoy";
    } else if ([Weather isEqualToString:@"阴有小雪转多云"]) {
        weatherImg=@"qns_weather_duoy";
    } else if ([Weather isEqualToString:@"阴有小雨"]) {
        weatherImg=@"qns_weather_xiaoy";
    } else if ([Weather isEqualToString:@"中到大雪"]) {
        weatherImg=@"qns_weather_dax";
    } else if ([Weather isEqualToString:@"多云"]) {
        weatherImg=@"qns_weather_duoy";
    } else if ([Weather isEqualToString:@"浮尘"]) {
        weatherImg=@"qns_weather_fuc";
    } else {
        weatherImg=@"qns_weather_qing";
    }
    return weatherImg;
}
- (MainSearchViewController *)menuContainerViewController {
    return (MainSearchViewController *)self.parentViewController;
}
-(void)GotoWeatherIndex:(UIButton *)button{
    WeatherIndexViewController *indexController=[[WeatherIndexViewController alloc] init];
    indexController.city=city;
    indexController.query=query;
    [[self  menuContainerViewController].navigationController pushViewController:indexController animated:YES];
}

//日期格式转换
-(NSString *)DateFormat:(NSString *)Date{
    NSDateFormatter *inputFormatter = [[NSDateFormatter alloc] init];
    [inputFormatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"en_US"]];
    [inputFormatter setDateFormat:@"yyyyMMdd"];
    NSDate* inputDate = [inputFormatter dateFromString:Date];
    
    NSDateFormatter *outputFormatter =[[NSDateFormatter alloc] init];
    [outputFormatter setLocale:[NSLocale currentLocale]];
    [outputFormatter setDateFormat:@"yyyy年MM月dd日"];
    NSString *strDate = [outputFormatter stringFromDate:inputDate];
    return strDate;
}
- (IBAction)PressPoint:(id)sender {
    WeatherIndexViewController *indexController=[[WeatherIndexViewController alloc] init];
    NSString *str=[[[[self.arrReturn objectForKey:@"Links" ] objectForKey:@"Link"] objectAtIndex:0] objectForKey:@"href"];
    NSRange range = [str rangeOfString:@"query="];
    str=[str substringFromIndex:range.location +range.length];
    indexController.query=str;
    [[self  menuContainerViewController].navigationController pushViewController:indexController animated:YES];
}
@end
